using System.Collections.Generic;
using UnityEngine;

public class ShopItemUI : GameUI
{
	[SerializeField]
	private ShopItemPrefab currentlySelectedItem;
	public UIGrid grid;

	public UIScrollView scrollView;

	public ShopItemPrefab shopItemPrefab;

	[SerializeField]
	private List<ShopItemPrefab> currentListing;

	private UISprite sprShowing;

	public UISprite spriteIcon;

	public UISprite sprButtonPlayer;

	public UISprite sprButtonWeapons;

	public UISprite sprButtonPowerUps;

	public UILabel lbName;

	public UILabel lbHowToUse;

	public UILabel lbCoin;

	public UILabel lbInapp;

	public UIGrid gridBuy;

	public GameObject groupWeapon;

	public GameObject groupPowerUp;

	public GameObject groupSpecialBundle;

	public GameObject groupBuy;

	public GameObject groupUse;

	public GameObject buttonUse;

	public GameObject buttonCoin;

	public GameObject buttonInapp;

	public IAPManager inappManager;

	[SerializeField]
	private ShopItemType showType;

	private Weapon showingWeapon;

	private PowerUp showingPowerUp;

	[Header("Weapons Group")]
	public UISprite sprDamage;

	public UISprite sprROF;

	public UISprite sprRecoil;

	public UILabel lbAmmoCount;

	public UILabel lbUpgradePrice;

	public UILabel lbAddUpgrade;

	public UILabel lbUpgradeDesc;

	public UILabel lbLevel;

	public GameObject groupUpgrade;

	[Header("Power-Ups Group")]
	public UILabel lbAmount;

	public UILabel lbDesc;

	[Header("Special Packs Group")]
	public UILabel lbLongDesc;

	private void Awake()
	{
		inappManager.onPurchaseSuccess += OnPurchaseSuccess;
	}

	public void ShowShop()
	{
		MyAdManager.Instance.MyShowNative();
		Show();
		ShowWeapons();
	}

	public void HideShop()
	{
		Hide();
	}

	public void ShowWeapons()
	{
		Show(ShopItemType.Weapon);
	}

	public void ShowPlayerData()
	{
		Show(ShopItemType.Player);
	}

	public void ShowPowerUps()
	{
		Show(ShopItemType.PowerUp);
	}

	public void ShowSpecialPacks()
	{
		Show(ShopItemType.SpecialBundle);
	}

	private void Show(ShopItemType type)
	{
		if (showType != type)
		{
			if (currentListing == null)
			{
				currentListing = new List<ShopItemPrefab>();
			}
			if (sprShowing != null)
			{
				sprShowing.width = 200;
				sprShowing.transform.GetChild(0).GetComponent<UILabel>().color = Color.white;
			}
			showType = type;
			switch (showType)
			{
				case ShopItemType.Weapon:
					sprShowing = sprButtonWeapons;
					clearGrid();
					foreach (Weapon weapon in Storage.instance.weapons)
					{
						ShopItemPrefab shopItemPrefab2 = Object.Instantiate(this.shopItemPrefab, grid.transform);
						shopItemPrefab2.SetItem(this, weapon);
						currentListing.Add(shopItemPrefab2);
					}
					break;
				case ShopItemType.Player:
					sprShowing = sprButtonPlayer;
					break;
				case ShopItemType.PowerUp:
					sprShowing = sprButtonPowerUps;
					clearGrid();
					foreach (PowerUp powerUp in Storage.instance.powerUps)
					{
						ShopItemPrefab shopItemPrefab = Object.Instantiate(this.shopItemPrefab, grid.transform);
						shopItemPrefab.SetItem(this, powerUp);
						currentListing.Add(shopItemPrefab);
					}
					break;
			}
			sprShowing.width = 275;
			sprShowing.transform.GetChild(0).GetComponent<UILabel>().color = new Color32(252, 98, 33, byte.MaxValue);
			if (currentListing.Count > 0)
			{
				currentListing[0].ClickItem();
			}
			Invoke("resetPosition", 0.02f);
		}
	}

	private void clearGrid()
	{
		if (grid.transform.childCount != 0)
		{
			for (int num = grid.transform.childCount - 1; num >= 0; num--)
			{
				UnityEngine.Object.Destroy(grid.transform.GetChild(num).gameObject);
			}
			currentListing.Clear();
		}
	}

	private void resetPosition()
	{
		grid.Reposition();
		scrollView.ResetPosition();
	}

	public void ShowItemData(Weapon weapon)
	{
		showType = ShopItemType.Weapon;
		groupWeapon.SetActive(value: true);
		groupPowerUp.SetActive(value: false);
		groupSpecialBundle.SetActive(value: false);
		showingWeapon = weapon;
		lbName.text = weapon.itemName;
		spriteIcon.spriteName = weapon.sprName;
		spriteIcon.MakePixelPerfect();
		sprDamage.fillAmount = weapon.damage / 100f;
		sprROF.fillAmount = 1f - weapon.shootCooldown;
		sprRecoil.fillAmount = weapon.recoilForce / 500f;
		lbAmmoCount.text = "[FFFFFFFF]" + weapon.baseAmmo + "[-]";
		if (weapon.Level > 0)
		{
			UILabel uILabel = lbAmmoCount;
			string text = uILabel.text;
			uILabel.text = text + " [00BDFFFF]+" + weapon.Level * weapon.upgradeAmmo + "[-]";
		}
		lbUpgradePrice.text = weapon.priceUpgrade + string.Empty;
		lbAddUpgrade.text = "升级\n+" + weapon.upgradeAmmo + " 子弹";
		if (spriteIcon.height > 70)
		{
			float d = 70f / (float)spriteIcon.height;
			spriteIcon.transform.localScale = new Vector3(-1f, 1f, 1f) * d;
		}
		bool flag = PersistenData.checkHasItem(weapon.PrefKey);
		if (flag)
		{
			lbLevel.text = weapon.Level + 1 + string.Empty;
		}
		else
		{
			lbLevel.text = "未解锁";
		}
		CheckShowItemUnlocked(flag);
		ShowPrices(weapon.priceCoin, weapon.priceInapp);
		if (weapon.Level >= 9)
		{
			lbUpgradeDesc.color = Color.red;
			lbUpgradeDesc.text = "这件武器已达最大等级";
			groupUpgrade.SetActive(value: false);
		}
		else
		{
			lbUpgradeDesc.color = Color.green;
			lbUpgradeDesc.text = "你不能升级这件武器";
			groupUpgrade.SetActive(value: true);
		}
	}

	public void ShowItemData(PowerUp powerUpData)
	{
		showType = ShopItemType.PowerUp;
		groupWeapon.SetActive(value: false);
		groupPowerUp.SetActive(value: true);
		groupSpecialBundle.SetActive(value: false);
		showingPowerUp = powerUpData;
		lbName.text = powerUpData.itemName;
		spriteIcon.spriteName = powerUpData.sprName;
		spriteIcon.MakePixelPerfect();
		lbAmount.text = powerUpData.Amount + string.Empty;
		lbDesc.text = powerUpData.desc;
		ShowNumberPowerUp();
		ShowPrices(powerUpData.price, 0f);
	}

	private void ShowPrices(int coin, float inapp)
	{
		buttonCoin.SetActive(coin > 0);
		buttonInapp.SetActive(inapp > 0f);
		lbCoin.text = coin + string.Empty;
		lbInapp.text = "$" + inapp;
		gridBuy.Reposition();
	}

	public void BuyWithInapp()
	{
		inappManager.Purchase(showingWeapon.sku);
	}

	private void OnPurchaseSuccess(string sku)
	{
		if (!(showingWeapon == null) && !string.IsNullOrEmpty(showingWeapon.sku) && showingWeapon.sku.Equals(sku))
		{
			UnlockProcess();
		}
	}

	public void BuyWithCoin()
	{
		if (showType == ShopItemType.Weapon)
		{
			if (PersistenData.Coin >= showingWeapon.priceCoin)
			{
				PersistenData.Coin -= showingWeapon.priceCoin;
				UnlockProcess();
				Tip.Show("购买成功");
			}
			else
			{
				Tip.Show("购买失败");
			}
		}
		else if (showType == ShopItemType.PowerUp && PersistenData.Coin >= showingPowerUp.price)
		{
			PersistenData.Coin -= showingPowerUp.price;
			UnlockProcess();
			Tip.Show("购买成功");
		}
		else
		{
			Tip.Show("购买失败");
		}

	}

	private void UnlockProcess()
	{
		switch (showType)
		{
			case ShopItemType.Weapon:
				PersistenData.addNewPurchaseData(showingWeapon.PrefKey);
				CheckShowItemUnlocked(unlocked: true);
				ShowItemData(showingWeapon);
				break;
			case ShopItemType.PowerUp:
				showingPowerUp.Amount++;
				ShowNumberPowerUp();
				break;
		}
	}

	private void CheckShowItemUnlocked(bool unlocked)
	{
		foreach (ShopItemPrefab item in currentListing)
		{
			item.UpdateShowItem();
		}
		groupBuy.SetActive(!unlocked);
		groupUse.SetActive(unlocked);
	}

	private void ShowNumberPowerUp()
	{
		groupBuy.SetActive(value: true);
		groupUse.SetActive(value: false);
		lbAmount.text = showingPowerUp.Amount + string.Empty;
	}

	public void UpgradeWeapon()
	{
		if (PersistenData.Coin >= showingWeapon.priceUpgrade)
		{
			foreach (Weapon weapon in Storage.instance.weapons)
			{
				if (weapon.id == showingWeapon.id)
				{
					PersistenData.Coin -= showingWeapon.priceUpgrade;
					weapon.Level++;
					ShowItemData(weapon);
					break;
				}
			}
		}
	}

	public void EquipWeapon()
	{
		if (showType == ShopItemType.Weapon)
		{
			if (PersistenData.UsingWeapon != showingWeapon.id)
			{
				PersistenData.UseWeapon(showingWeapon.id);
			}
			else
			{
				PersistenData.UseWeapon(-1);
			}
			ShowItemData(showingWeapon);
			foreach (ShopItemPrefab item in currentListing)
			{
				item.ShowUsing();
			}
		}
	}
	public void OnItemSelected(ShopItemPrefab selectedItem)
	{
		// 取消之前选中项的效果
		if (currentlySelectedItem != null && currentlySelectedItem != selectedItem)
		{
			currentlySelectedItem.SetSelected(false);
		}

		// 设置新的选中项
		currentlySelectedItem = selectedItem;
		currentlySelectedItem.SetSelected(true);
	}
	private void MarkCurrentItemAsSelected()
	{
		if (currentListing == null) return;

		foreach (ShopItemPrefab item in currentListing)
		{
			bool isThisItem = false;

			switch (showType)
			{
				case ShopItemType.Weapon:
					isThisItem = (showingWeapon != null && item.weaponData == showingWeapon);
					break;
				case ShopItemType.PowerUp:
					isThisItem = (showingPowerUp != null && item.powerUpData == showingPowerUp);
					break;
			}

			if (isThisItem)
			{
				OnItemSelected(item);
				break;
			}
		}
	}
}
